<?php
namespace app\api\controller;
use app\common\BaseController;
use think\Db;
use app\api\model\Receipt as ReceiptModel;
use app\api\model\Payment as PaymentModel;
use app\api\model\Transfer as TransferModel;

class ReceiptFlow extends BaseController{    
    public function __construct(){
        parent::__construct();       
    }
    public function index(){  
        $rwhere=[];    
        $twhere=[];     
        $params = $this->request->param();       
        $page = 1;
        $pageCount = 10;        
        $datas=[];
        if(!empty($params)){      
             //按收付款日期查询
             if(!empty($params['start_date']) && !empty($params['end_date'])){
                $rwhere['receipt_date'] = [['>=',$params['start_date']],['<=',$params['end_date']]];              
                $twhere['transfer_date'] = [['>=',$params['start_date']],['<=',$params['end_date']]];             
            }else{
                if(!empty($params['start_date'])){
                    $rwhere['receipt_date'] = ['>= ',$params['start_date']]; 
                    $twhere['transfer_date'] = ['>= ',$params['start_date']];                   
                }
                if(!empty($params['end_date'])){
                    $rwhere['receipt_date'] = ['<=',$params['end_date']];                    
                    $twhere['transfer_date'] = ['<=',$params['end_date']];                      
                }
            }     
           
            //页码            
            $page = !empty($params['page']) ? $params['page'] : $page;
            $pageCount = !empty($params['page_count']) ? $params['page_count'] : $pageCount;
            //按客户查询
            if(!empty($params['client_id'])){
                $rwhere['client_id'] = $params['client_id'];        
            }  
            //按科目查询
            if(!empty($params['subject_id'])){
                $rwhere['subject_id'] = $params['subject_id'];                   
                $twhere['to_bank'] = $params['subject_id'];                
            }         
        }  
        $Payment = new PaymentModel();
       $Receipt = new ReceiptModel();
        $Transfer = new TransferModel();
        $a = $Receipt->alias('a')
        ->field("a.in,a.receipt_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.client_id,b.guest_id,b.salesman_id,c.start_date,c.sn,c.title")
        ->join('lt_team_income b',['a.income_id=b.id','a.type=b.type'])
        ->join('lt_team c','b.parent_id=c.id')
        ->where($rwhere)
        ->buildSql();               
        $b = $Receipt->alias('a')
        ->field("a.in,a.receipt_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.client_id,b.guest_id,b.salesman_id,b.start_date,b.sn,b.product_name as title")
        ->join('lt_single b',['a.income_id=b.id','a.type=b.type'])
        ->where($rwhere)
        ->buildSql(); 
        $c = $Transfer->alias('a')
        ->field("a.to_in,a.transfer_date as price_date,a.to_bank as subject_id,a.to_money as money,a.to_number as invoice_num,x_id as remark,x_id as client_id,x_id as guest_id,x_id as salesman_id,a.start_date,a.sn,a.title")        
        ->where($twhere)      
        ->buildSql(); 
        $datas = Db::table($a.' a')->union([$b,$c])->page("$page,$pageCount")->orderRaw("CAST(invoice_num as decimal)")->select();    

        $c_a = $Receipt->alias('a')
        ->field("a.in,a.receipt_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.client_id,b.guest_id,b.salesman_id,c.start_date,c.sn,c.title")
        ->join('lt_team_income b',['a.income_id=b.id','a.type=b.type'])
        ->join('lt_team c','b.parent_id=c.id')
        ->where($rwhere)
        ->count();               
        $c_b = $Receipt->alias('a')
        ->field("a.in,a.receipt_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.client_id,b.guest_id,b.salesman_id,b.start_date,b.sn,b.product_name as title")
        ->join('lt_single b',['a.income_id=b.id','a.type=b.type'])
        ->where($rwhere)
        ->count();        
        $c_c = $Transfer->alias('a')
        ->field("a.to_in,a.transfer_date as price_date,a.to_bank as subject_id,a.to_money as money,a.to_number as invoice_num,x_id as remark,x_id as client_id,x_id as guest_id,x_id as salesman_id,a.start_date,a.sn,a.title")        
        ->where($twhere)      
        ->count();        
     
        $datalist = array(
            'datas' => $datas,            
            'dataCount' => $c_a+$c_b+$c_c
        );
        return $datalist;     
    }   
    public function cvs(){  
        $rwhere=[];    
        $twhere=[];     
        $params = $this->request->param();       
        $page = 1;
        $pageCount = 10;        
        $datas=[];
        if(!empty($params)){      
             //按收付款日期查询
             if(!empty($params['start_date']) && !empty($params['end_date'])){
                $rwhere['receipt_date'] = [['>=',$params['start_date']],['<=',$params['end_date']]];              
                $twhere['transfer_date'] = [['>=',$params['start_date']],['<=',$params['end_date']]];             
            }else{
                if(!empty($params['start_date'])){
                    $rwhere['receipt_date'] = ['>= ',$params['start_date']]; 
                    $twhere['transfer_date'] = ['>= ',$params['start_date']];                   
                }
                if(!empty($params['end_date'])){
                    $rwhere['receipt_date'] = ['<=',$params['end_date']];                    
                    $twhere['transfer_date'] = ['<=',$params['end_date']];                      
                }
            }     
           
            //页码            
            $page = !empty($params['page']) ? $params['page'] : $page;
            $pageCount = !empty($params['page_count']) ? $params['page_count'] : $pageCount;
            //按客户查询
            if(!empty($params['client_id'])){
                $rwhere['client_id'] = $params['client_id'];        
            }  
            //按科目查询
            if(!empty($params['subject_id'])){
                $rwhere['subject_id'] = $params['subject_id'];                   
                $twhere['to_bank'] = $params['subject_id'];                
            }         
        }  
        $Payment = new PaymentModel();
       $Receipt = new ReceiptModel();
        $Transfer = new TransferModel();
        $a = $Receipt->alias('a')
        ->field("a.in,a.receipt_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.client_id,b.guest_id,b.salesman_id,c.start_date,c.sn,c.title")
        ->join('lt_team_income b',['a.income_id=b.id','a.type=b.type'])
        ->join('lt_team c','b.parent_id=c.id')
        ->where($rwhere)
        ->buildSql();               
        $b = $Receipt->alias('a')
        ->field("a.in,a.receipt_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.client_id,b.guest_id,b.salesman_id,b.start_date,b.sn,b.product_name as title")
        ->join('lt_single b',['a.income_id=b.id','a.type=b.type'])
        ->where($rwhere)
        ->buildSql(); 
        $c = $Transfer->alias('a')
        ->field("a.to_in,a.transfer_date as price_date,a.to_bank as subject_id,a.to_money as money,a.to_number as invoice_num,x_id as remark,x_id as client_id,x_id as guest_id,x_id as salesman_id,a.start_date,a.sn,a.title")        
        ->where($twhere)      
        ->buildSql(); 
        $datas = Db::table($a.' a')->union([$b,$c])->orderRaw("CAST(invoice_num as decimal)")->select();    

        
        return $datas;     
    }   
}
